<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Kamo
 * Date: 01.07.13
 * Time: 21:48
 * To change this template use File | Settings | File Templates.
 */

namespace Modules\JasminumContact\Controllers;

use Gotcha\App\Extensions\GotchaCulturesExtension;
use Gotcha\App\Extensions\GotchaErrorsExtension;
use Gotcha\App\Extensions\GotchaLogsExtension;
use Gotcha\App\Extensions\HttpExtension;
use Gotcha\App\Framework\Controller;
use Gotcha\App\Types\String;
use GotchaDatabase\GotchaSettingsQuery;

class DefaultController extends Controller {
    public function Index() {
        return parent::View();
    }

    public function Send() {
        $http = new HttpExtension();

        // Validate input
        if(!empty($_POST['Fullname']) && !empty($_POST['Email'])) {
            $FullName = new String($_POST['Fullname']);
            $Email = new String($_POST['Email']);
            $Message = new String($_POST['Message']);

            if(strlen($FullName) > 50 || strlen($Email) > 50 || strlen($Message) > 5000 || strlen($FullName) < 5) {
                header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/kontakt?error=2'));
                exit;
            } else {
                strip_tags($FullName);
                strip_tags($Email);
                strip_tags($Message);

                if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
                    header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/kontakt?error=3'));
                    exit;
                }

                // Send email
                $settings = GotchaSettingsQuery::create()
                                ->findOneByStrongName('JasminumContactMail');

                // Prepare e-mail setup
                $mail = new \PHPMailer();

                $mail->IsSMTP(); // Use SMTP
                $mail->SMTPDebug = 0;
                $mail->SMTPAuth = true; // Use SMTP authentication
                $mail->SMTPSecure = "tls";
                $mail->Host = 'mail.restauracja-jasminum.pl';
                $mail->Port = 587;
                // $mail->Username = 'automat@codenova.drl.pl';
                $mail->Username = 'automat@restauracja-jasminum.pl';
                // $mail->Password = 'automat1';
                $mail->Password = ';Z;;gF(lBl++';
                $mail->CharSet = 'UTF-8';

                $mail->SetFrom('automat@codenova.drl.pl', 'Restauracja Jasminum - Automat');
                $mail->Subject = 'Strona Jasminum - Formularz kontaktowy';
                $mail->MsgHTML(sprintf('<h1>Wysłano mail</h1><table><tbody><tr><td><b>Imię i nazwisko:</b></td><td>%s</td></tr><tr><td><b>Adres e-mail:</b></td><td>%s</td></tr><tr><td><b>Treść wiadomości</b></td><td>%s</td></tr></tbody></table>', $FullName, $Email, $Message));

                // It is possible to add multiple emails
                // so be prepared!
                $mails = explode(';', $settings->getValue());

                foreach($mails as $mailAddress) {
                    if($mailAddress != '') {
                        $mail->AddAddress($mailAddress);
                    }
                }

                if (!$mail->Send()) {
                    GotchaLogsExtension::createNewLogEntry(new String($mail->ErrorInfo));
                    GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_SENDING_MAIL'))));
                    exit;
                }

                header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/kontakt?send=ok'));
                exit;
            }
        } else {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/kontakt?error=1'));
            exit;
        }
    }
}